package com.ithiema.reggie.controller;


import com.ithiema.reggie.common.R;
import com.ithiema.reggie.pojo.AliOssproperties;
import com.ithiema.reggie.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/common")
public class CommonController {
//文件上传
        @Value("${reggie.path}")
    private String basePath;
        @Autowired
    AliOSSUtils aliOSSUtils;
        @PostMapping("/upload")
    public R<String> upload(MultipartFile file) throws IOException {
            log.info(file.toString());
            //原始报名文件
            String originalFilename = file.getOriginalFilename();
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
            //使用UUID重新生成文件名，防止文件名重复造成覆盖
            String FileName = UUID.randomUUID().toString()+ suffix;
            log.info("文件上传，生成的文件名：{}",FileName);
            //创建目录对象
//            File dir = new File(basePath);
//            if (!dir.exists()){
//                dir.mkdir();
//            }
//            try {
//                file.transferTo(new File(basePath+FileName));
//            }catch (IOException e){
//                e.getStackTrace();
//
//            }
            //oss上传
      aliOSSUtils.uploadFile(FileName,file.getInputStream());
            return R.success(FileName);
        }
//        @GetMapping("/download")
//    public void download(String name, HttpServletResponse response){
//            try {
//                //输入流，读取文件内容
//                FileInputStream fileInputStream = new FileInputStream(new File(basePath + name));
//                //输出流，通过输出流将文件协会浏览器
//                ServletOutputStream outputStream = response.getOutputStream();
//                response.setContentType("image/jpeg");
//                int len = 0;
//                byte[] bytes = new byte[1024];
//                while ((len = fileInputStream.read(bytes)) !=-1){
//                    outputStream.write(bytes,0,len);
//                    outputStream.flush();
//                }
//                //关闭自愿
//                outputStream.close();
//                fileInputStream.close();
//
//            }catch (Exception e){
//                e.printStackTrace();
//            }
//        }
    @GetMapping("/download")
    public void download(String name, HttpServletResponse response) throws IOException {
            log.info("下载图片：{}",name);
            aliOSSUtils.downLoadFile(name,response.getOutputStream());
    }


}
